<template>
    <div class="mod-order" style="margin: 10px">
        <!--查询面板-->
        <SearchPanel ref="searchPanel" :selectOptions="selectOptions" :search-config="searchConfig" @startSearch="startSearch">
            <!-- <el-form-item>
                <el-button icon="el-icon-plus" type="primary" size="mini" @click="add">新增</el-button>
            </el-form-item> -->

            <!-- <el-form-item>
                <el-button type="danger" size="mini" @click="batchDel" :disabled="selectDataList.length == 0">批量删除</el-button>
            </el-form-item> -->
        </SearchPanel>

        <!-- 查询结果 -->
        <ResultListPanel
            tableRef="table"
            ref="multipleTable"
            :checkBoxEnable="true"
            :data="dataList ? dataList : []"
            :listConfig="resultColConfig"
            :listLoading="listLoading"
            @actionClick="actionClick"
            @paginationChange="paginationChange"
            :tagTextExchange="tagTextExchange"
            @selectionChange="selectionChange"
            :actionLimit="actionLimit"
            :actionLimitName="actionLimitName"
        ></ResultListPanel>
    </div>
</template>

<script>
import {
    INDEX_SEARCH_CONFIG,
    NCP_INDEX_SEARCH_RESULT_ITEMS,
    NZ_INDEX_SEARCH_RESULT_ITEMS,
    INDEX_SEARCH_SELECT_OPTIONS_CONFIG
} from './config';
import { isPostpone, isType,statusType } from '../../../utils/util';
import ResultListPanel from '../../../components/ResultListPanel';
import SearchPanel from '../../../components/SearchPanel';
import API from '@/api';
import cloneDeep from 'lodash/cloneDeep';

export default {
    components: {
        ResultListPanel,
        SearchPanel
    },
    data() {
        return {
            visible: false,
            selectDataList: [],
            // 搜索项配置
            searchConfig: INDEX_SEARCH_CONFIG,
            resultColConfig: [],
            listLoading: false, // 数据列表显示状态
            dataList: [], //  数据列表
            /* 分页查询数据 */
            pageSize: 10,
            pageNum: 1,
            totalPage: 0,
            qFlag: '0',
            /* 查询条件参数 */
            dataForm: {
                materialName: null,
                status: '0'
            },
            type: 1,
            selectOptions: INDEX_SEARCH_SELECT_OPTIONS_CONFIG
        };
    },

    mounted() {
        this.getList();
    },

    watch: {
        //- 解决 同一页面，数据不刷新问题

        // 监听相同路由下参数变化的时候，从而实现异步刷新
        $route(to, from) {
            this.getList();
            this.nickname = this.$route.query.nickname; //修改页面标题
        }
    },

    methods: {
        selectionChange(val) {
            this.selectDataList = val;
        },

        /***
         * 搜索面板触发 搜索事件
         * @param params
         */
        startSearch(params) {
            this.dataForm = params;
            this.pageNum = 1;
            this.$set(this.$refs.multipleTable['pageParams'], 'pageNum', 1);

            this.getList();
        },

        /***
         * 触发 列表 操作按钮 （ 编辑 和 删除 ）
         * @param data
         * @param item
         */
        actionClick([data, item]) {
            if (item.alias === 'audit') {
                data.opFlag = this.qFlag;
                this.Edit(data);
            }
        },

        // 编辑
        Edit(data) {
            switch (this.$route.path) {
                case '/goodsManger/publicProductsAudit/ncp/index':
                    //农产品管理

                    this.$router.push({
                        path: '/goodsManger/publicProductsAudit/ncp/audit',
                        query: { itemData: data, type: 2, pageType: 'NCP' }
                    });

                    break;

                case '/goodsManger/publicProductsAudit/nz/index':
                    //农资产品管理

                    this.$router.push({
                        path: '/goodsManger/publicProductsAudit/nz/audit',
                        query: { itemData: data, type: 2, pageType: 'NZ' }
                    });

                    break;
            }
        },

        tagTextExchange([data, item]) {
             //状态
            if (item.alias == 'status') {
                return statusType(data.status);
            }

        },

        actionLimit([data, obj]) {

            //操作 按钮显示隐藏  

            return true;
        },
        actionLimitName([data, action]){

             if (this.qFlag== '0') { //未审核，才能操作

                if (action.alias == 'audit' && data.flowStatusEnumStr == '审核通过(完成)') {
                    return "详情";
                } else {
                    return action.name;
                }

            } else { //已审核，看详情
                  
                 return "详情";
               
            }

        },

        /**
         * 分页器发生改变 触发事件
         * @param page
         */
        paginationChange(page) {
            this.pageSize = page.pageSize;
            this.pageNum = page.pageNo;
            this.getList();
        },

        // 列表 接口
        getList() {
            
            //处理列表 显示
            switch (this.$route.path) {
                case '/goodsManger/publicProductsAudit/ncp/index':
                    //农产品管理

                    this.resultColConfig = NCP_INDEX_SEARCH_RESULT_ITEMS;

                    break;

                case '/goodsManger/publicProductsAudit/nz/index':
                    //农资产品管理

                    this.resultColConfig = NZ_INDEX_SEARCH_RESULT_ITEMS;

                    break;
            }

            this.qFlag=this.dataForm.status;

            //处理列表 传递参数
            let params = cloneDeep(this.dataForm);
            params.pageNum = this.pageNum;
            params.pageSize = this.pageSize;

            this.listLoading = true;
            var tick;
            switch (this.$route.path) {
                case '/goodsManger/publicProductsAudit/ncp/index':
                    //农产品管理
                    tick = API.goodsManger.ncpAuditList(params);

                    break;

                case '/goodsManger/publicProductsAudit/nz/index':
                    //农资产品管理

                    tick = API.goodsManger.nzAuditList(params);

                    break;
            }

            tick.then((res) => {
                console.log(res);
                this.listLoading = false;
                this.totalPage = res.result.total;
                this.dataList = res.result.rows;
                this.$set(this.$refs.multipleTable['pageParams'], 'total', res.result.total);
            });
        }
    }
};
</script>

<style>
</style>